package com.xhwl.centiotdata.suppliersystem.dahuaIcc;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.dto.dahua.PassengerFlowDTO;
import com.xhwl.centiotdata.pojo.dto.passengerflow.PassengerFlowIntegrationConfigDTO;
import com.xhwl.common.constant.cent.NumberConstants;
import com.xhwl.common.exception.DataException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PassengerFlowHandler {

    private static final String BASE_URL = "/evo-apigw/evo-passengerflow/1.0.0/statistics/onTimeReport/now/list";

    @Autowired
    private GeneralDaHuaIccHandler generalDaHuaIccHandler;

    public List<PassengerFlowDTO> passengerFlowDTOS(PassengerFlowIntegrationConfigDTO passengerFlowIntegrationConfigDTO,
                                                    int pageNum,int pageSize, Integer regionId) {
        Map<String, Object> stringObjectMap = new HashMap<>();
        stringObjectMap.put("regionId", regionId);
        stringObjectMap.put("pageNum", pageNum);
        stringObjectMap.put("pageSize", pageSize);
        JSONObject jsonObject = generalDaHuaIccHandler.get(BASE_URL, stringObjectMap, 0, 0,
                passengerFlowIntegrationConfigDTO.getIpAndPort(), passengerFlowIntegrationConfigDTO.getLoginName(),
                passengerFlowIntegrationConfigDTO.getPassword(),
                passengerFlowIntegrationConfigDTO.getAk(), passengerFlowIntegrationConfigDTO.getSk());
        List<PassengerFlowDTO> passengerFlowDTOS = new ArrayList<>();
        if (jsonObject != null){
            String code = jsonObject.getString("code");
            if (NumberConstants.TWO_HUNDRED_STR.equals(code)) {
                // 成功
                JSONObject dataJson = jsonObject.getJSONObject("data");
                if (dataJson == null) {
                    return passengerFlowDTOS;
                }
                JSONObject innerDataJson = dataJson.getJSONObject("data");
                if (innerDataJson == null) {
                    return passengerFlowDTOS;
                }
                JSONArray jsonArray = innerDataJson.getJSONArray("pageData");
                passengerFlowDTOS = jsonArray.toJavaList(PassengerFlowDTO.class);
                return passengerFlowDTOS;
            } else {
                log.error("获取餐厅客流数据返回码错误:{}",jsonObject);
                throw new DataException(jsonObject.getString("errMsg"));
            }
        }else {
            log.error("获取餐厅客流数据返回为空");
            return passengerFlowDTOS;
        }

    }

}
